//
//  PickupVideoView.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/3/9.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

class PickupVideoView: UIView {
    
    var pickerupClosure: (() -> Void)?
    var rejectClosure: (()  -> Void)?
    
    @IBOutlet private weak var titleLabel: UILabel!
    
    @IBOutlet private weak var avatarView: UIImageView! {
        didSet {
            avatarView.contentMode = .scaleAspectFill
            avatarView.layer.cornerRadius = avatarView.height / 2
            avatarView.layer.masksToBounds = true
        }
    }

    @IBAction private func pickerupAction() {
        dismiss()
        pickerupClosure?()
    }
    
    @IBAction private func rejectAction() {
//        dismiss()
        rejectClosure?()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.alpha = 0.0
    }
    
    func show(in view: UIView? = nil, model: ReceiveModel) {
        let superview = view == nil ? mainWindow : view!
        superview.addSubview(self)
        
        self.frame = superview.bounds
        
        titleLabel.text = model.nameAndOffice
        loadAvatar(model.doctorUserId)
        
        UIView.animate(withDuration: 0.25, delay: 0, options: .curveLinear, animations: {
            self.alpha = 1.0
        })
    }
    
    func dismiss() {
        UIView.animate(withDuration: 0.25, delay: 0, options: .curveLinear, animations: {
            self.alpha = 0.0
        }) { _ in
            self.removeFromSuperview()
        }
    }
    
    private func loadAvatar(_ doctorUserId: String) {
        let params = ["userId": doctorUserId.intValue]
        HttpTool.shared.request(target: ApiManager.doctorPersonInfo(paras: params), success: { (json, code) in
            YSGLog("加载医生头像 json: \(json)")
            self.avatarView.kf.setImage(with: URL(string: json["data"]["avatarUrl"].description),
                                        placeholder: UIImage(named: "video_avatar_default"))
        }) { error in
            YSGLog("加载医生头像失败: \(error ?? "")")
        }
    }
}
